package com.lhczf.lucenedb.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author: 刘华春
 * @date: 2019/2/6
 */
@Component
public class BeanUtil implements ApplicationContextAware {

    private static BeanUtil beanUtil;
    private static ApplicationContext applicationContext;

    private BeanUtil() {
    }

    public static BeanUtil getInstance() {
        synchronized (BeanUtil.class) {
            if (beanUtil == null) {
                beanUtil = new BeanUtil();
            }
        }
        return beanUtil;
    }

    @Override
    public void setApplicationContext(ApplicationContext ac) {
        applicationContext = ac;
    }

    private ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }
}
